/*
 * Copyright (C) 2004-2006  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <stdafx.h>
#include <OWS/FdoOwsGetCapabilities.h>
#include <OWS/FdoOwsResponse.h>
#include "FdoWcsDelegate.h"
#include "FdoWcsServiceMetadata.h"
#include "FdoWcsGetCoverage.h"
#include "FdoWcsXmlGlobals.h"
#include "FdoWcsDescribeCoverage.h"

FdoWcsDelegate::FdoWcsDelegate() :
     FdoOwsDelegate(NULL, NULL, NULL)
{
}

FdoWcsDelegate::FdoWcsDelegate(FdoString* defaultUrl, FdoString* userName, FdoString* passwd) :
     FdoOwsDelegate(defaultUrl, userName, passwd)
{
}

FdoWcsDelegate::~FdoWcsDelegate()
{
}

void FdoWcsDelegate::Dispose()
{ 
    delete this; 
}

FdoWcsDelegate* FdoWcsDelegate::Create(FdoString* defaultUrl, FdoString* userName, FdoString* passwd)
{
    return new FdoWcsDelegate(defaultUrl, userName, passwd);
}

FdoWcsServiceMetadata* FdoWcsDelegate::GetServiceMetadata(FdoString* pVersion)
{
    FdoPtr<FdoOwsGetCapabilities> request = FdoOwsGetCapabilities::Create(FdoWcsXmlGlobals::WCSServiceName);
    if (pVersion == NULL || wcslen(pVersion) == 0)
        request->SetVersion(FdoWcsXmlGlobals::WcsVersion);
    else
        request->SetVersion(L""); // version already in URL.
    FdoPtr<FdoOwsResponse> response = Invoke(request);
    FdoPtr<FdoIoStream> stream = response->GetStream();
    FdoWcsServiceMetadataP rv = FdoWcsServiceMetadata::Create();
    rv->ReadXml(stream); //enable it when xml parse is ready!

    return FDO_SAFE_ADDREF(rv.p);
}

FdoIoStream* FdoWcsDelegate::GetCoverage(FdoString* identifier, FdoSize height,FdoSize width,FdoOwsBoundingBox* bbox, FdoString* format, 
                                         FdoBoolean store,
                                         FdoWcsGridCRS* gridCRS,
                                         FdoWcsTimeSequence* timeSequence,
                                         /*TODO: RangeSubSet*/
                                         FdoString* version)
{	


    FdoStringP crs = bbox->GetCRS ();
	FdoDouble minX = bbox->GetMinX ();
	FdoDouble minY = bbox->GetMinY ();
	FdoDouble maxX = bbox->GetMaxX ();
	FdoDouble maxY = bbox->GetMaxY ();

    FdoPtr<FdoWcsGetCoverage> request = FdoWcsGetCoverage::Create(identifier, format,crs,height,width,minX,minY,maxX,maxY, store, gridCRS, timeSequence, version);
    FdoPtr<FdoOwsResponse> response = Invoke (request);
	FdoPtr<FdoIoStream> stream = response->GetStream ();

	return FDO_SAFE_ADDREF (stream.p);
}

FdoWcsCoverageDescriptions* FdoWcsDelegate::DescribeCoverage(FdoStringCollection *identifiers, FdoString *version)
{
    VALIDATE_ARGUMENT(identifiers);
    VALIDATE_ARGUMENT(version);

    FdoWcsDescribeCoverageP request = FdoWcsDescribeCoverage::Create(identifiers, version);
    FdoPtr<FdoOwsResponse> response = Invoke(request);
    FdoPtr<FdoIoStream> stream = response->GetStream();
    FdoWcsCoverageDescriptionsP coverageDescs = FdoWcsCoverageDescriptions::Create();
    coverageDescs->ReadXml(stream);

    return FDO_SAFE_ADDREF(coverageDescs.p);
}

//FdoIoStream* FdoWcsDelegate::GetMap(FdoStringCollection* layerNames,
//									FdoStringCollection* styleNames,
//									FdoWmsBoundingBox* bbox, 
//									FdoString* imgFormat,
//									FdoSize height,
//									FdoSize width,
//									FdoBoolean bTransparent,
//									FdoString* backgroundColor,
//									FdoString* timeDimension,
//									FdoString* elevation,
//									FdoString* version)
//{
//	VALIDATE_ARGUMENT (layerNames);
//	VALIDATE_ARGUMENT (styleNames);
//	VALIDATE_ARGUMENT (bbox);
//	VALIDATE_ARGUMENT (imgFormat);
//	VALIDATE_ARGUMENT (backgroundColor);
//	VALIDATE_ARGUMENT (timeDimension);
//	VALIDATE_ARGUMENT (elevation);
//	
//	FdoStringP crs = bbox->GetCRS ();
//	FdoDouble minX = bbox->GetMinX ();
//	FdoDouble minY = bbox->GetMinY ();
//	FdoDouble maxX = bbox->GetMaxX ();
//	FdoDouble maxY = bbox->GetMaxY ();
//
//	FdoPtr<FdoWmsGetMap> request = FdoWmsGetMap::Create (layerNames, styleNames, crs, imgFormat, height, width, minX, minY, maxX, maxY, version, bTransparent, backgroundColor, timeDimension, elevation);
//    FdoPtr<FdoOwsResponse> response = Invoke (request);
//	FdoPtr<FdoIoStream> stream = response->GetStream ();
//
//	return FDO_SAFE_ADDREF (stream.p);
//}
